package com.remote.server.Asyn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.remote.server.service.BlackService;
import com.smartcar.common.domain.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Log4j2
public class TongbuController {

    @Autowired
    private BlackService blackService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostConstruct
    public void task() {
        blackService.selectUser().stream().forEach(t ->  redisTemplate.opsForList().leftPush("user", JSONObject.toJSONString(t)) );
        List<String> user = redisTemplate.opsForList().range("user", 0, -1);
        User user1 = user
                .stream()
                .map(t -> {
                    return JSON.parseObject(t, User.class);}
                )
                .filter(t -> t.getId() == 2)
                .findFirst()
                .get();
        System.out.println(user1);
    }


}
